package com.snail.readyvr.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.Html;
import android.text.Spanned;

import com.github.pwittchen.networkevents.library.ConnectivityStatus;
import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.mvp.model.AccountInfo;
import com.snail.readyvr.mvp.model.User;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wangyunpeng on 2016/3/10.
 * 工具类
 */
public class Utils {


    public static ConnectivityStatus sConnectivityStatus = ConnectivityStatus.UNKNOWN;

    public static String sessionid = "";
    public static String token = "";
    public static User sUser = new User();
    public static boolean isLive = false;

    public static AccountInfo myAccountInfo = null;

    public static final String VR_IMG_URL = "file:///android_asset/splash_pano.jpg";

    public static final String USER_AGREEMENT_URL = "http://mr18.readyvr.co/#/about?id=7";

    private Utils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static boolean hasConnected(ConnectivityStatus connectivityStatus) {
        if (connectivityStatus == ConnectivityStatus.OFFLINE || connectivityStatus == ConnectivityStatus.UNKNOWN)
            return false;
        return true;
    }

    public static boolean hasMobileConnected(ConnectivityStatus connectivityStatus) {
        if (connectivityStatus == ConnectivityStatus.MOBILE_CONNECTED)
            return true;
        return false;
    }

    public static boolean hasWIFIConnected(ConnectivityStatus connectivityStatus) {
        if (connectivityStatus == ConnectivityStatus.WIFI_CONNECTED || connectivityStatus == ConnectivityStatus.WIFI_CONNECTED_HAS_INTERNET || connectivityStatus == ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET)
            return true;
        return false;
    }

    public static boolean checkNetWork(Activity activity) {
        boolean hasNetWork = Utils.hasConnected(Utils.sConnectivityStatus);
        if (!hasNetWork)
            TBSnackBarUtils.showShort(activity, R.string.no_network);
        return hasNetWork;
    }

    public static Spanned getUnderLineText(String content) {
        return Html.fromHtml("<u>" + content + "</u>");
    }

    public static boolean isMobileNum(String mobiles) {
        Pattern p = Pattern.compile("^((17[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 根据错误码，获得错误提示
     *
     * @param errorCode
     * @return
     */
    public static String getErrorTip(int errorCode) {
        switch (errorCode) {
            case 3001:
                return MyApplication.getmContext().getString(R.string.code_3001);
            case 3002:
                return MyApplication.getmContext().getString(R.string.code_3002);
            case 3003:
                return MyApplication.getmContext().getString(R.string.code_3003);
            case 3004:
                return MyApplication.getmContext().getString(R.string.code_3004);
            case 3005:
                return MyApplication.getmContext().getString(R.string.code_3005);
            case 3006:
                return MyApplication.getmContext().getString(R.string.code_3006);
            case 3007:
                return MyApplication.getmContext().getString(R.string.code_3007);
            case 3008:
                return MyApplication.getmContext().getString(R.string.code_3008);
            case 3009:
                return MyApplication.getmContext().getString(R.string.code_3009);
            case 3010:
                return MyApplication.getmContext().getString(R.string.code_3010);
            case 3011:
                return MyApplication.getmContext().getString(R.string.code_3011);
            case 3012:
                return MyApplication.getmContext().getString(R.string.code_3012);
            case 3013:
                return MyApplication.getmContext().getString(R.string.code_3013);
            default:
                return MyApplication.getmContext().getString(R.string.code_default);
        }

    }

    /**
     * 检测是否是视频地址
     *
     * @param text
     * @return
     */

    public static boolean isVideoUrl(String text) {
        return !StringUtils.isNull(text) && (text.startsWith("rtmp://") || ((text.startsWith("http://") || text.startsWith("https://")) && (text.contains("m3u8") || text.contains("mp4") || text.contains("flv"))));
    }

    public static boolean checkPsw(String psw) {
        Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$");
        Matcher m = p.matcher(psw);
        return m.matches();
    }

    /**
     * 保留小数点后一位
     *
     * @param num
     * @return
     */
    public static float formatFloat(float num) {
        BigDecimal b = new BigDecimal(num);
        return b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    /**
     * 保留小数点后二位
     *
     * @param num
     * @return
     */
    public static float formatFloat2(float num) {
        BigDecimal b = new BigDecimal(num);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public static String formatVideoLength(long length) {
        String strLength = null;

        long curLength = length / 1024 / 1024;
        if ((curLength >= 1024) && (curLength % 1024 == 0)) {
            strLength = curLength / 1024 + "GB";
        } else if (curLength > 1024) {
            strLength = formatFloat2((float) curLength / (float) 1024) + "GB";
        } else
            strLength = curLength + "MB";

        return strLength;
    }

    public static int getHeadIconRes(String name) {
        if (StringUtils.isNull(name))
            return R.drawable.protrait_1;
        if (name.equals("system1"))
            return R.drawable.protrait_1;
        else if (name.equals("system2"))
            return R.drawable.protrait_2;
        else if (name.equals("system3"))
            return R.drawable.protrait_3;
        else if (name.equals("system4"))
            return R.drawable.protrait_4;
        else if (name.equals("system5"))
            return R.drawable.protrait_5;
        else if (name.equals("system6"))
            return R.drawable.protrait_6;
        return R.drawable.protrait_1;
    }

    /**
     *
     * @param context
     * @return
     */
    public static Locale getLocal(Context context){
        if(context == null)
            return Locale.ENGLISH;
        String languageName = (String) SPUtils.get(context,Constant.Language.LANGUAGE_KEY, "");

        if (languageName == null || languageName.equals(""))
            languageName = Utils.getDefaultLanguage(context);
        if(languageName != null){
            if (languageName.equals("CN"))
                return Locale.SIMPLIFIED_CHINESE;
            else if (languageName.equals("TW") || languageName.equals("HK"))
                return Locale.TRADITIONAL_CHINESE;
            else if (languageName.equals("ja"))
                return Locale.JAPANESE;
            else if (languageName.equals("en"))
                return Locale.ENGLISH;
            else
                return Locale.ENGLISH;
        }

        return Locale.ENGLISH;
    }

    /**
     *进Unity时要加个参数：lang 取值：english,simplified,traditional,japanese
     * @param context
     * @return
     */
    public static String getUnityLang(Context context){
        if(context == null)
            return "english";
        String languageName = (String) SPUtils.get(context,Constant.Language.LANGUAGE_KEY, "");

        if (languageName == null || languageName.equals(""))
            languageName = Utils.getDefaultLanguage(context);
        if(languageName != null){
            if (languageName.equals("CN"))
                return "simplified";
            else if (languageName.equals("TW") || languageName.equals("HK"))
                return "traditional";
            else if (languageName.equals("ja"))
                return "japanese";
            else if (languageName.equals("en"))
                return "english";
            else
                return "english";
        }

        return "english";
    }

    /**
     * 改变应用的语言
     *
     * @param context
     * @param language
     */
    public static void changeLanuage(Context context, String language) {
        if (context == null || language == null)
            return;
        LogUtils.d("changeLanuage-->" + language);
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        if (language.equals("CN"))
            configuration.locale = Locale.SIMPLIFIED_CHINESE;
        else if (language.equals("TW") || language.equals("HK"))
            configuration.locale = Locale.TRADITIONAL_CHINESE;
        else if (language.equals("ja"))
            configuration.locale = Locale.JAPANESE;
        else if (language.equals("en")) {
            Locale locale = Locale.ENGLISH;
            locale.setDefault(locale);
            configuration.locale = locale;
        }
        else
            configuration.locale = Locale.ENGLISH;

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }

    /**
     * zh--TW     zh--HK    zh--CN   en--US     ja--JP
     * Locale locale = getResources().getConfiguration().locale;
     * LogUtils.d(locale.getLanguage() + "--" + locale.getCountry());
     *
     * @param context
     * @return
     */
    public static String getDefaultLanguage(Context context) {
        if (context == null)
            return null;
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage() + "--" + locale.getCountry();
        if (language.endsWith("CN"))
            return Constant.Language.LANGUAGE_CN_VALUE;
        else if (language.endsWith("TW") || language.endsWith("HK"))
            return Constant.Language.LANGUAGE_HK_VALUE;
        else if (language.startsWith("en"))
            return Constant.Language.LANGUAGE_EN_VALUE;
        else if (language.startsWith("ja"))
            return Constant.Language.LANGUAGE_JA_VALUE;

        return Constant.Language.LANGUAGE_EN_VALUE;
    }

    /**
     * 获取语言索引
     *
     * @param language
     * @return
     */
    public static int getLanguageIndex(String language) {

        if (language == null)
            return 0;
        if (language.equals(Constant.Language.LANGUAGE_CN_VALUE))
            return 0;
        else if (language.equals(Constant.Language.LANGUAGE_HK_VALUE))
            return 1;
        else if (language.equals(Constant.Language.LANGUAGE_EN_VALUE))
            return 2;
        else if (language.equals(Constant.Language.LANGUAGE_JA_VALUE))
            return 3;

        return 0;
    }

    /**
     * 根据索引获取语言
     *
     * @param index
     * @return
     */
    public static String getLanuageByIndex(int index) {
        switch (index) {
            case 0:
               return Constant.Language.LANGUAGE_CN_VALUE;
            case 1:
               return Constant.Language.LANGUAGE_HK_VALUE;
            case 2:
               return Constant.Language.LANGUAGE_EN_VALUE;
            case 3:
               return Constant.Language.LANGUAGE_JA_VALUE;
        }
        return null;
    }

}
